﻿using IotClientApp.Server.Devices;
using IotClientApp.Shared;
using Microsoft.AspNetCore.SignalR;

namespace IotClientApp.Server.Hubs
{
    public class ParamMonitorHub : Hub
    {
        private readonly DeviceClientHost _deviceClientHost;
        public ParamMonitorHub(DeviceClientHost deviceClientHost)
        {
            _deviceClientHost = deviceClientHost;
        }

        public async Task SendMessage(string user, string message)
        {
            await Clients.All.SendAsync("ReceiveMessage", user, message);
        }
        private DeviceClient deviceClient;
        public async Task SendRealData(string deviceId)
        {
            deviceClient = _deviceClientHost.GetDevice(deviceId);
            if (deviceClient != null)
            {
                deviceClient.SendToHubClient(Context.ConnectionId, Clients.Client(Context.ConnectionId));
            }
        }
        public override async Task OnDisconnectedAsync(Exception? exception)
        {
            if (deviceClient != null)
            {
                deviceClient.RemoveHubClient(Context.ConnectionId);
            }
        }
    }
}